# Contributor: Oleg A. Khlybov <fougas@mail.ru>

_realname=superlu
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.2.2
pkgrel=2
arch=('any')
pkgdesc="Sparse direct linear solver (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc")
optdepends=("${MINGW_PACKAGE_PREFIX}-tcl: build & run test suite")
options=('strip' 'staticlibs')
license=('3-clause BSD')
url='https://portal.nersc.gov/project/sparse/superlu/'
source=("https://portal.nersc.gov/project/sparse/superlu/${_realname}_${pkgver}.tar.gz"
        'all.tcl'
        'tclbuildtest.tcl'
        'superlu.test'
        '0001-pc-fix.patch')
sha256sums=('470334a72ba637578e34057f46948495e601a5988a602604f5576367e606a28c'
            'ec5072630e1c0309fe383669e9187790cd135a393c67bc4bc35cf60b0ba396ff'
            '15c7af25b91406d5fe5f26cfe00963b6cfde1c3dd466eb25f1b6fae299934966'
            '5efeea2586552dcbccf238c4aed94d64525e33fb9eca8741b56a961a6fd8e144'
            '86c7a2854632d83524d6f4e29d0dc08197de30c337e6e916922cb21623e1cfa7')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  for p in ${source[*]:4}; do
    patch -p1 -i "${srcdir}/$p"
  done
}

build() {
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi
  cd "${srcdir}"
  mkdir -p build-${CARCH}s && cd build-${CARCH}s
  MSYS2_ARG_CONV_EXCL='-DCMAKE_INSTALL_PREFIX=' \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${_btype} \
      -DBUILD_SHARED_LIBS=OFF \
      -DBUILD_TESTING=OFF \
      -Denable_internal_blaslib=OFF \
      ../${_realname}-${pkgver}
  make
  cd "${srcdir}"
  mkdir -p build-${CARCH} && cd build-${CARCH}
  MSYS2_ARG_CONV_EXCL='-DCMAKE_INSTALL_PREFIX=' \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${_btype} \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_NEED_RESPONSE=ON \
      -DBUILD_TESTING=OFF \
      -Denable_internal_blaslib=OFF \
      ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}/build-${CARCH}s"
  make DESTDIR="${pkgdir}" install
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install
  mkdir -p "${pkgdir}/${MINGW_PREFIX}"/bin
  cp SRC/libsuperlu.dll "${pkgdir}/${MINGW_PREFIX}"/bin
  mkdir -p "${pkgdir}/${MINGW_PREFIX}/share/test/${_realname}"
  cd "${pkgdir}${MINGW_PREFIX}/include"
  mkdir "${_realname}"
  find . -type f -exec mv {} "${_realname}" \;
  cd "${srcdir}"
  cp {all,tclbuildtest}.tcl ${_realname}.test "${pkgdir}/${MINGW_PREFIX}/share/test/${_realname}"
  cd "${_realname}-${pkgver}/EXAMPLE"
  cp ?linsol.c g20.rua cg20.cua "${pkgdir}/${MINGW_PREFIX}/share/test/${_realname}"
}
